package com.itheima.d2_reflect_test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;

/*
    对象框架
    目标：保存任意对象的字段和其数据到文件中去
 */
public class ObjectFrame {
    public static void saveOBJ(Object obj) throws Exception {
        Class clazz=obj.getClass();

        BufferedWriter bw=new BufferedWriter(new FileWriter("user.txt",true));
        bw.write("==================="+clazz.getSimpleName()+"=======================");
        bw.newLine();
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            bw.write(declaredField.getName()+"=");
            bw.write(declaredField.get(obj).toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
